home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / SoundModel.as < prev    next >
Text File  |  2008-11-04  |  6KB  |  211 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import flash.events.ErrorEvent;
  7.    import flash.events.Event;
  8.    import flash.events.IOErrorEvent;
  9.    import flash.events.ProgressEvent;
  10.    import flash.media.ID3Info;
  11.    import flash.media.Sound;
  12.    import flash.media.SoundChannel;
  13.    import flash.media.SoundLoaderContext;
  14.    import flash.media.SoundTransform;
  15.    import flash.net.URLRequest;
  16.    import flash.utils.clearInterval;
  17.    import flash.utils.setInterval;
  18.    
  19.    public class SoundModel implements ModelInterface
  20.    {
  21.        
  22.       
  23.       private var channel:SoundChannel;
  24.       
  25.       private var interval:Number;
  26.       
  27.       private var sound:Sound;
  28.       
  29.       private var duration:Number;
  30.       
  31.       private var model:Model;
  32.       
  33.       private var transform:SoundTransform;
  34.       
  35.       private var context:SoundLoaderContext;
  36.       
  37.       private var position:Number;
  38.       
  39.       public function SoundModel(param1:Model)
  40.       {
  41.          super();
  42.          model = param1;
  43.          transform = new SoundTransform();
  44.          if(model.config["mute"] == true)
  45.          {
  46.             volume(0);
  47.          }
  48.          else
  49.          {
  50.             volume(model.config["volume"]);
  51.          }
  52.          context = new SoundLoaderContext(model.config["bufferlength"] * 1000);
  53.       }
  54.       
  55.       public function stop() : void
  56.       {
  57.          clearInterval(interval);
  58.          if(channel)
  59.          {
  60.             channel.stop();
  61.          }
  62.          try
  63.          {
  64.             sound.close();
  65.          }
  66.          catch(err:Error)
  67.          {
  68.          }
  69.       }
  70.       
  71.       private function timeHandler() : void
  72.       {
  73.          var _loc1_:* = undefined;
  74.          var _loc2_:* = undefined;
  75.          position = Math.round(channel.position / 100) / 10;
  76.          _loc1_ = Math.round(sound.length * sound.bytesTotal / sound.bytesLoaded / 100) / 10;
  77.          if(sound.isBuffering == true && sound.bytesTotal > sound.bytesLoaded)
  78.          {
  79.             if(model.config["state"] != ModelStates.BUFFERING)
  80.             {
  81.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  82.             }
  83.             else
  84.             {
  85.                _loc2_ = Math.floor(sound.length / (channel.position + model.config["bufferlength"] * 1000) * 100);
  86.                model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc2_});
  87.             }
  88.          }
  89.          else if(model.config["state"] == ModelStates.BUFFERING && sound.isBuffering == false)
  90.          {
  91.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  92.          }
  93.          if(_loc1_ > position)
  94.          {
  95.             model.sendEvent(ModelEvent.TIME,{
  96.                "position":position,
  97.                "duration":_loc1_
  98.             });
  99.          }
  100.          if(_loc1_ != duration && !isNaN(_loc1_))
  101.          {
  102.             duration = _loc1_;
  103.             model.sendEvent(ModelEvent.META,{"duration":duration});
  104.          }
  105.       }
  106.       
  107.       private function completeHandler(param1:Event) : void
  108.       {
  109.          clearInterval(interval);
  110.          position = model.playlist[model.config["item"]]["start"];
  111.          model.sendEvent(ModelEvent.TIME,{
  112.             "position":position,
  113.             "duration":duration
  114.          });
  115.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  116.       }
  117.       
  118.       public function volume(param1:Number) : void
  119.       {
  120.          transform.volume = param1 / 100;
  121.          if(channel)
  122.          {
  123.             channel.soundTransform = transform;
  124.          }
  125.       }
  126.       
  127.       private function progressHandler(param1:ProgressEvent) : void
  128.       {
  129.          var _loc2_:* = undefined;
  130.          var _loc3_:* = undefined;
  131.          _loc2_ = param1.bytesLoaded;
  132.          _loc3_ = param1.bytesTotal;
  133.          model.sendEvent(ModelEvent.LOADED,{
  134.             "loaded":_loc2_,
  135.             "total":_loc3_
  136.          });
  137.       }
  138.       
  139.       private function errorHandler(param1:ErrorEvent) : void
  140.       {
  141.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  142.          stop();
  143.       }
  144.       
  145.       public function load() : void
  146.       {
  147.          position = model.playlist[model.config["item"]]["start"];
  148.          duration = model.playlist[model.config["item"]]["duration"];
  149.          sound = new Sound();
  150.          sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  151.          sound.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  152.          sound.addEventListener(Event.ID3,id3Handler);
  153.          sound.load(new URLRequest(model.playlist[model.config["item"]]["file"]),context);
  154.          play();
  155.       }
  156.       
  157.       private function id3Handler(param1:Event) : void
  158.       {
  159.          var id3:ID3Info = null;
  160.          var obj:* = undefined;
  161.          var evt:Event = param1;
  162.          try
  163.          {
  164.             id3 = sound.id3;
  165.             obj = {
  166.                "type":"id3",
  167.                "album":id3.album,
  168.                "artist":id3.artist,
  169.                "comment":id3.comment,
  170.                "genre":id3.genre,
  171.                "songName":id3.songName,
  172.                "track":id3.track,
  173.                "year":id3.year
  174.             };
  175.             model.sendEvent(ModelEvent.META,obj);
  176.          }
  177.          catch(err:Error)
  178.          {
  179.          }
  180.       }
  181.       
  182.       public function play() : void
  183.       {
  184.          channel = sound.play(position * 1000,0,transform);
  185.          channel.removeEventListener(Event.SOUND_COMPLETE,completeHandler);
  186.          channel.addEventListener(Event.SOUND_COMPLETE,completeHandler);
  187.          interval = setInterval(timeHandler,100);
  188.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  189.       }
  190.       
  191.       public function pause() : void
  192.       {
  193.          clearInterval(interval);
  194.          channel.stop();
  195.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  196.       }
  197.       
  198.       public function seek(param1:Number) : void
  199.       {
  200.          clearInterval(interval);
  201.          position = param1;
  202.          channel.stop();
  203.          play();
  204.       }
  205.       
  206.       public function quality(param1:Boolean) : void
  207.       {
  208.       }
  209.    }
  210. }
  211.